package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.group.Goods;
import com.pinyougou.mapper.*;
import com.pinyougou.pojo.*;
import com.pinyougou.pojo.TbGoodsExample.Criteria;
import com.pinyougou.sellergoods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private TbGoodsMapper goodsMapper;
	@Autowired
    private TbGoodsDescMapper goodsDescMapper;
	@Autowired
	private TbItemCatMapper tbItemCatMapper;
	@Autowired
	private TbBrandMapper brandMapper;
	@Autowired
	private TbSellerMapper sellerMapper;
	@Autowired
	private TbItemMapper itemMapper;
	/**
	 * 查询全部
	 */
	@Override
	public List<TbGoods> findAll() {
		return goodsMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbGoods> page=   (Page<TbGoods>) goodsMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Goods goods) {
	    //先添加商品列表
		goodsMapper.insert(goods.getGoods());
		//添加商品描述
        goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());
        //插入goods列表
        goodsDescMapper.insert(goods.getGoodsDesc());
        //得到item列表对象
		List<TbItem> items = goods.getItems();
		//训话遍历
		//设置属性
		addItem(goods);

	}

	private void addItem(Goods goods) {
		for (TbItem item : goods.getItems()) {
			//设置商品id
			item.setGoodsId(goods.getGoods().getId());
			//得到品牌id
			Long brandId = goods.getGoods().getBrandId();
			//根据品牌id得到品牌对象的名字
			String brand = brandMapper.selectByPrimaryKey(brandId).getName();
			//设置品牌名字
			item.setBrand(brand);
			//设置分类id
			item.setCategoryid(goods.getGoods().getCategory3Id());
			//根据三级分类id查询得到分类的名字
			String tbItemCat = tbItemCatMapper.selectByPrimaryKey(goods.getGoods().getCategory3Id()).getName();
			//设置分类名字
			item.setCategory(tbItemCat);
			//设置创建时间
			item.setCreateTime(new Date());
			//设置更新时间
			item.setUpdateTime(new Date());
			//得到商家id
			String sellerId = goods.getGoods().getSellerId();
			//根据商家id查询得到商家店铺名称
			String nickName = sellerMapper.selectByPrimaryKey(sellerId).getNickName();
			//设置商家店铺名称
			item.setSeller(nickName);
			//设置标题
			item.setTitle(goods.getGoods().getGoodsName());
			//先从goodsdesc中得到json的字符串，里面有图片url
			String itemImages = goods.getGoodsDesc().getItemImages();
			//将json字符串转成list集合
			List<Map> maps = JSON.parseArray(itemImages, Map.class);
			//判断maps是否为空
			if(maps.size() > 0 && maps != null){
				//得到图片url
				String url = (String) maps.get(0).get("url");
				//设置图片url
				item.setImage(url);
			}
			//插入item对象
			itemMapper.insert(item);
		}
	}


	/**
	 * 修改
	 */
	@Override
	public void update(Goods goods){
		//先修改goods表
		goodsMapper.updateByPrimaryKey(goods.getGoods());
		//再修改goodsDesc表
		goodsDescMapper.updateByPrimaryKey(goods.getGoodsDesc());
		//再修改tb_item表，（先删除原来的数据，再添加新的数据）
		//删除原来的数据
		TbItemExample example=new TbItemExample();
		TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andGoodsIdEqualTo(goods.getGoods().getId());
		itemMapper.deleteByExample(example);
		//再重新添加新的数据
		addItem(goods);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Goods findOne(Long id){
		//根据商品id查询指定的商品信息
		TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
		//根据商品id查询指定的商品描述信息
		TbGoodsDesc tbGoodsDesc = goodsDescMapper.selectByPrimaryKey(id);
		//查询指定的sku商品列表
		TbItemExample example=new TbItemExample();
		TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andGoodsIdEqualTo(id);
		List<TbItem> tbItems = itemMapper.selectByExample(example);
		//定义商品组合对象
		Goods goods = new Goods();
		//给组合对象赋值
		goods.setGoods(tbGoods);
		goods.setGoodsDesc(tbGoodsDesc);
		goods.setItems(tbItems);
		//返回
		return goods;
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			goodsMapper.deleteByPrimaryKey(id);
		}		
	}
	
	
		@Override
	public PageResult findPage(TbGoods goods, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbGoodsExample example=new TbGoodsExample();
		Criteria criteria = example.createCriteria();
		
		if(goods!=null){			
						if(goods.getSellerId()!=null && goods.getSellerId().length()>0){
				criteria.andSellerIdLike("%"+goods.getSellerId()+"%");
			}
			if(goods.getGoodsName()!=null && goods.getGoodsName().length()>0){
				criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
			}
			if(goods.getAuditStatus()!=null && goods.getAuditStatus().length()>0){
				criteria.andAuditStatusLike("%"+goods.getAuditStatus()+"%");
			}
			if(goods.getIsMarketable()!=null && goods.getIsMarketable().length()>0){
				criteria.andIsMarketableLike("%"+goods.getIsMarketable()+"%");
			}
			if(goods.getCaption()!=null && goods.getCaption().length()>0){
				criteria.andCaptionLike("%"+goods.getCaption()+"%");
			}
			if(goods.getSmallPic()!=null && goods.getSmallPic().length()>0){
				criteria.andSmallPicLike("%"+goods.getSmallPic()+"%");
			}
			if(goods.getIsEnableSpec()!=null && goods.getIsEnableSpec().length()>0){
				criteria.andIsEnableSpecLike("%"+goods.getIsEnableSpec()+"%");
			}
			if(goods.getIsDelete()!=null && goods.getIsDelete().length()>0){
				criteria.andIsDeleteLike("%"+goods.getIsDelete()+"%");
			}
	
		}
		
		Page<TbGoods> page= (Page<TbGoods>)goodsMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void updateStatus(Long[] ids, String status) {

	}

	@Override
	public List<TbItem> findItemListByGoodsIdandStatus(Long[] goodsId, String status) {
		return null;
	}

	@Override
	public List<TbItemCat> findByParenId(String parentId) {
		TbItemCatExample example=new TbItemCatExample();

		TbItemCatExample.Criteria criteria = example.createCriteria();

		criteria.andParentIdEqualTo(Long.parseLong(parentId));

		List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
		return tbItemCats;
	}

}
